package com.baozun.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 
 * @Title:
 * @Company:宝尊电子商务有限公司
 * @Description:
 * @Author:xiaozhou.zhou
 * @Since:2014年12月30日
 * @Copyright:Copyright (c) 2014
 * @ModifyDate:
 * @Version:1.1.0
 */
public class SessionUtil {
    
    /**
     * 
     * @param request
     * @param sessionAttr
     * @param obj
     * @Description:
     */
    public static void setSession(HttpServletRequest request, String sessionAttr, Object obj) {
        // TODO Auto-generated method stub
        HttpSession session = request.getSession();
        setSession(session, sessionAttr, obj);
    }
    
    /**
     * \
     * 
     * @param session
     * @param sessionAttr
     * @param obj
     * @Description:
     */
    public static void setSession(HttpSession session, String sessionAttr, Object obj) {
        session.setAttribute(sessionAttr, obj);
    }
    
    /**
     * 
     * @param request
     * @param sessionAttr
     * @Description:
     */
    public static void remove(HttpServletRequest request, String sessionAttr) {
        HttpSession session = request.getSession();
        setSession(session, sessionAttr, null);
    }
    
    /**
     * 
     * @param session
     * @param sessionAttr
     * @Description:
     */
    public static void remove(HttpSession session, String sessionAttr) {
        setSession(session, sessionAttr, null);
    }
    
    /**
     * 
     * @param session
     * @param class1
     * @return 
     * @return
     * @Description:
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAttribute(HttpSession session, String attrName, Class<T> t) {
        // TODO Auto-generated method stub
        T bean;
        try {
            bean = t.newInstance();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bean = (T)session.getAttribute(attrName);
        return bean;
    }
    
    public static <T> T getAttribute(HttpServletRequest request, String attrName, Class<T> clazz) {
        // TODO Auto-generated method stub
        return getAttribute(request.getSession(), attrName, clazz);
    }
}
